package com.compass.mobile.presenters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.leanback.widget.Presenter
import com.compass.mobile.R
import com.compass.mobile.models.Nurse
import dp2px
import setUrlRound

/**
 * Description： 护理Item
 * Date:2024/5/21 10:35
 */
class NurseItemPresenter : Presenter() {
    private var finalWidth: Int? = null
    fun setFinalWidth(finalWidth: Int?) {
        this.finalWidth = finalWidth
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup): ViewHolder {
        val view =
            LayoutInflater.from(viewGroup.context).inflate(R.layout.item_case, viewGroup, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(viewHolder: Presenter.ViewHolder, item: Any) {
        if (item is Nurse) {
            val vh = viewHolder as ViewHolder
            vh.layoutParams.height = (finalWidth!! * 0.8).toInt()
            vh.itemView.layoutParams = vh.layoutParams
            vh.tvTitle.text = item.title
            vh.tvValue.text = "￥${item.price}/元"
            item.imgs?.let {
                if (it.isNotEmpty()){
                    vh.imgNurse.setUrlRound(it[0],dp2px(10f))
                }

            }
        }
    }

    override fun onUnbindViewHolder(viewHolder: Presenter.ViewHolder) {}
    class ViewHolder(view: View) : Presenter.ViewHolder(view) {
        var tvTitle: TextView
        var tvValue: TextView
        var imgNurse: ImageView
        var itemView: ConstraintLayout
        var layoutParams: ViewGroup.LayoutParams

        init {
            tvTitle = view.findViewById(R.id.tv_title)
            tvValue = view.findViewById(R.id.tvValue)
            imgNurse = view.findViewById(R.id.imgNurse)
            itemView = view.findViewById(R.id.itemView)
            layoutParams = itemView.layoutParams
        }
    }
}
